/*
 * Copyright (C) 2009 Las Cumbres Observatory (lcogt.net)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.lcogt.tools.fitsviewer.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.lcogt.tools.fitsviewer.client.TreeDataService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class TreeDataServiceImpl extends RemoteServiceServlet implements TreeDataService {
	String rootPath;
	String jpgRootFile;
	Properties prop;
	Process p;
	public  String[] getTreeData(String fileName) {
		//Finds path on server
		if(prop == null)
		{
			prop = new Properties();
			ServletContext context = getServletContext();
			InputStream is = context.getResourceAsStream("FitsViewer.properties");
			try {
				prop.load(is);
				rootPath = prop.getProperty("serverPathOfFitsFiles");
				jpgRootFile = prop.getProperty("serverPathOfJPGS");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//checks if any files are Fits Files. Also checks if there is a cooresponding JPG. If not it creates one
		String path = rootPath + fileName+"/";
		File rootFile = new File(path);
		String[] files = rootFile.list();
		for(int i = 0; i < files.length;i++)
		{
			if(files[i].contains(".fits"))
			{
				
				File jpgFile = new File(jpgRootFile+fileName+"/jpg/"+parseFitsToString(files[i]));
				if(!(jpgFile.exists()))
				{
					p =JPGCreator.createJPG(jpgRootFile+fileName+"/jpg/"+parseFitsToString(files[i]),rootFile+"/"+files[i],getServletContext().getRealPath(""));
					try {
						if(p.waitFor()!=0)
							files[i]="";
					} catch (InterruptedException e) {
						files[i]="";
					}
				}
				
				
			}
			else if(files[i].contains(".") || files[i].contains("jpg")|| files[i].contains("ZoomImages"))
			{
				files[i]="";
			}
		}
		//Sorts Files in reverse alphabetical order so newest images are shown on top
		Arrays.sort(files,new FitsComparator());
		/*
		for(int i = 0;i < files.length/2;i++)
		{
			String first = files[i];
			String second = files[files.length-i-1];
			files[i] = second;
			files[files.length-i-1]=first;
		}
		*/
		return files;
	}
	//Returns fits name into cooresponding jpg name
	public String parseFitsToString(String input)
	  {
		return input.replace(".fits", ".jpg");
	  }

	

}
